package com.afteryuan.server;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.core.write.WriteRequest;
import org.apache.mina.filter.logging.LoggingFilter;

import java.util.Date;

import com.afteryuan.server.config.Configuration;
import com.afteryuan.server.data.SceneLoader;
import com.afteryuan.server.data.DataLoader;
import com.afteryuan.server.thread.SynManager;

/**
 * Server类 socket服务器的主入口
 * 初始化socket服务器，开通socket服务。socket服务端口10088
 * <p/>
 * <p/>
 * <p><a href="Server.java.html"><i>View Source</i></a></p>
 *
 * @author <a href="mailto:afteryuan@afteryuan.com">$Author: Spires Zhang $</a>
 * @version $Revision: 1.52 $
 */
public class Server {
    private static Log log = LogFactory.getLog(Server.class);

    /**
     * 主程序入口
     *
     * @param args 命令行参数
     * @throws Exception 所有异常
     */
    public static void main(String[] args) throws Exception {
        long start = new Date().getTime();
        //开始启动服务器
        //1.加载系统配置
        //todo 此处直接加载
        String serverFrame = MainThread.MINA_FRAME;
        Configuration config = Configuration.getInstance();
        System.out.println("Server Configuration has loaded.");

        //2.加载Spring Context;
        SpringContextLoader.getInstance();
        System.out.println("Spring Context has Loaded.");

        //3.准备数据.如场景数据,etc
        DataLoader dataLoader = (DataLoader) SpringContextLoader.getInstance().getBeanFactory().getBean("dataLoader");
        dataLoader.init();
        System.out.println("Server Data has Loaded.");
        
        //4.启动服务主线程
        MainThread thread = new MainThread(serverFrame,config);
        new Thread(thread).start();

        //5.启动同步管理器
        Thread synManager = new SynManager();
//        synManager.start();


        
        long end = new Date().getTime();
        System.out.println("Server has started in "+ (end-start)/1000+" secend.");
    }
}